package com.atguigu.edu.controller;


import com.atguigu.edu.client.VodClient;
import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.entity.vo.CourseInfoVo;
import com.atguigu.edu.entity.vo.CoursePublishVo;
import com.atguigu.edu.entity.vo.CourseQuery;
import com.atguigu.edu.service.CourseService;
import com.atguigu.edu.service.VideoService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-07-06
 */
@RestController
@Api(description = "课程信息及课程描述-8001")
@RequestMapping("/edu/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @ApiOperation("添加课程信息及课程描述")
    @PostMapping("/addcourse")
    public R insertCourseInfoDescription(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.addCourse(courseInfoVo);
        if (!StringUtils.isEmpty(id)) {
            return R.ok().message("添加成功").data("courseId", id);
        } else {
            return R.error().message("添加失败,请检查数据").data("courseId", null);
        }
    }

    @ApiOperation("根据id查询课程信息及课程描述")
    @GetMapping("/getcourse/{courseId}")
    public R getCourseById(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseOne(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @ApiOperation("根据id查询课程信息及课程描述")
    @PutMapping("/editcourse")
    public R getCourseById(@RequestBody CourseInfoVo courseInfoVo) {
        if (courseInfoVo != null) {
            try {
                boolean flag = courseService.updateCourseAndCourseDescriptionsById(courseInfoVo);
                if (flag) {
                    return R.ok().message("修改成功!");
                } else {
                    return R.error().message("修改失败!");
                }
            } catch (Exception e) {
                return R.error().message("修改失败!");
            }
        }
        return R.ok().message("修改成功!");
    }

    @ApiOperation("根据id查询 最终发布对象")
    @GetMapping("/getPublishVo/{courseId}")
    public R coursePublishVo(@PathVariable String courseId) {

        try {
            CoursePublishVo coursePublishVo = courseService.getPublishById(courseId);
            return R.ok().data("coursePublishVo", coursePublishVo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("根据id最终发布")
    @GetMapping("/publish/{courseId}")
    public R coursePublish(@PathVariable String courseId) {
        try {
            Course course = new Course();
            course.setId(courseId);
            course.setStatus("Normal");
            boolean b = courseService.updateById(course);
            if (b) {
                return R.ok();
            } else {
                return R.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("查询所有course")
    @PostMapping("/getAll/{pageNum}/{limit}")
    public R courseAll(@PathVariable long pageNum, @PathVariable long limit, @RequestBody(required = false) CourseQuery courseQuery) {
        Page<Course> page = new Page<Course>(pageNum, limit);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            wrapper.like("title", courseQuery.getTitle());
        }
        if (!StringUtils.isEmpty(courseQuery.getStatus())) {
            wrapper.eq("status", courseQuery.getStatus());
        }
        if (!StringUtils.isEmpty(courseQuery.getStart())) {
            wrapper.ge("gmt_create", courseQuery.getStart());
        }
        if (!StringUtils.isEmpty(courseQuery.getEnd())) {
            wrapper.le("gmt_create", courseQuery.getEnd());
        }
        wrapper.orderByDesc("gmt_create");
        IPage<Course> iPage = courseService.page(page, wrapper);
        List<Course> list = iPage.getRecords();
        long total = iPage.getTotal();
        return R.ok().data("courseList", list).data("total", total);
    }

    @Autowired
    VodClient vodClient;
    @Autowired
    VideoService videoService;

    @ApiOperation("删除course")
    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        if (StringUtils.isEmpty(courseId)) {
            return R.error().message("参数不能为空!");
        }
        if (!StringUtils.isEmpty(courseId)) {

            boolean b = courseService.deleteCourseById(courseId);
            if (b) {
                QueryWrapper<Video> wrapper = new QueryWrapper<>();
                wrapper.eq("course_id", courseId);
                List<Video> list = videoService.list(wrapper);
                if (list != null && list.size() > 0) {
                    ArrayList<String> ids = new ArrayList<>();
                    for (Video video : list) {
                        ids.add(video.getVideoSourceId());
                    }
//            如果上述未出现异常 进行远程调用删除对应的 视频存储
//        OpenFeign 远程调用
                    vodClient.delVideoByList(ids);
                    ids.clear();
                }
            }
        }
        return R.ok();
    }


}
